<?php
namespace app\service;
use app\exception\CommonException;
use app\model\Market;
use think\facade\Cache;
use think\facade\Log;

class MarketService extends BaseService
{
    const PRIMARY = 1; //首页
    protected function setModel(): string
    {
        return Market::class;
    }

    public function getClosePriceByUsdt($coinId,$num):float
    {
         $payCoinsService = app()->make(PayCoinService::class);
         $res = $payCoinsService->get($coinId);
         if (!$res) {
             throw new CommonException("币种不存在");
         }
         if (strtolower($res->coin_name)=="usdt") {
             return $num;
         }
         $closePriceKey =  sprintf("market.%susdt.kline.%s",$res->coin_name,"1sec");
         try {
             $data = Cache::store('redis')->get($closePriceKey);

             return $data["close"]*$num;
         } catch (\Exception $e) {
             Log::info("redis获取对应的币种交易价格失败",['key'=>$closePriceKey,"error"=>$e->getMessage()]);
             $price = $res->close_price;
             return $price*$num;
         }
    }
}